package com.whcoding.base.project.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.whcoding.base.project.annotion.AutoLog;
import com.whcoding.base.project.common.api.BaseResultVO;
import com.whcoding.base.project.common.api.ResultStatusEnum;
import com.whcoding.base.project.constant.BusinessTypeEnum;
import com.whcoding.base.project.pojo.OrmDeptVO;
import com.whcoding.base.project.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author Sanilthink
 */
@RestController
@RequestMapping("/dept/")
public class DeptController {
	private static final Logger log = LoggerFactory.getLogger(DeptController.class);

	@Autowired
	private DeptService deptService;

	/**
	 * http://172.16.57.141:9800/basic_project/dept/hello
	 *
	 * @param name
	 * @return
	 * @GetMapping = RequestMapping(method=HttpMethod.Get)
	 */
	@GetMapping("/hello")
	@AutoLog(value = "我是Hello方法",businessType= BusinessTypeEnum.OTHER)
	public String sayHello(@RequestParam(required = false, name = "name") String name) {
		if (StrUtil.isBlank(name)) {
			name = "whcoding";
		}
		return StrUtil.format("Hello World, {}!", name);
	}

	/**
	 * 请求地址 ：
	 * http://172.16.57.141:9800/basic_project/dept/queryDeptById?id=1
	 * <p>
	 * 根据ID 查询数据
	 * defaultValue 设置参数默认值
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("queryDeptById")
	public BaseResultVO queryDeptById(@RequestParam(value = "id", defaultValue = "0") Long id) {
		log.info("方法queryDeptById,请求参数" + String.valueOf(id));
		if (Objects.isNull(id)) {
			return BaseResultVO.fail(ResultStatusEnum.PARAM_IS_INVALID.getCode(), ResultStatusEnum.PARAM_IS_INVALID.getMessage());
		}
		log.error("方法queryDeptById ,请求打印错误日志记录！");
		return BaseResultVO.success(deptService.queryDeptById(id));
	}



	/**
	 * 新增数据
	 *
	 * @param deptVO
	 * @return
	 */
	@PostMapping("add")
	public boolean addDept(@RequestBody OrmDeptVO deptVO) {
		return deptService.addDept(deptVO);
	}

	/**
	 * 更新数据
	 *
	 * @param deptVO
	 * @return
	 */
	@PostMapping("updateDept")
	public boolean updateDept(@RequestBody OrmDeptVO deptVO) {
		return deptService.updateDept(deptVO);
	}

	/**
	 * 删除数据
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping("deleteDeptById")
	public ResponseEntity deleteDeptById(@RequestParam("id") Long id) {
		Boolean resultState = deptService.deleteDeptById(id);
		return ResponseEntity.ok(resultState);
	}

	/**
	 * 查询全部数据
	 *
	 * @return
	 */
	@GetMapping("queryAllDept")
	public List<OrmDeptVO> queryAllDept() {
		List<OrmDeptVO> deptVOS= deptService.queryAllDept();
		String resultJosn=JSON.toJSONString(deptVOS);
		System.out.println(resultJosn);
		return deptService.queryAllDept();
	}

	/**
	 * 删除/更新
	 * <p>
	 * http://172.16.57.141:9800/basic_project/dept/insertOrUpdateBatch?id=1
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("insertOrUpdateBatch")
	public ResponseEntity insertOrUpdateBatch(@RequestParam("id") Long id) {
		deptService.insertOrUpdateBatch(id);
		return ResponseEntity.ok().build();
	}

}
